package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.shared.Category;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class Categories {
	private TextBox name;
	private Button save,saveAsNew,delete;
	private Label errorMsg;
	private int editingId = -1; 
	private CellTable<Category> table = new CellTable<Category>();
	
	final ServiceAsync myService = (ServiceAsync)
	GWT.create(Service.class);
	final AsyncCallback<ArrayList<Category>> getCallback = new AsyncCallback<ArrayList<Category>>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Error in getCallback Categories.");
		}
		@Override
		public void onSuccess(ArrayList<Category> result) {
			table.setRowCount(result.size(),false);
			table.setRowData(result);
		}
	};
	final AsyncCallback<Boolean> updateCallback = new AsyncCallback<Boolean>() {
		@Override
		public void onFailure(Throwable caught) {
			System.out.println("Error in updateCallback Categories.");
		}
		@Override
		public void onSuccess(Boolean result) {
			if (result) {
				errorMsg.setText("De categorie is succesvol opgeslagen.");
			} else {
				errorMsg.setText("Er ging iets mis probeer het opnieuw.");
			}
		}
	};
	
	public Categories() {
	}
	
	public Panel createPage() {
		//create panels
		VerticalPanel p = new VerticalPanel();
		
		FlexTable layoutTable = new FlexTable();
		layoutTable.addStyleName("nicetable");
		
		errorMsg = new Label("");
		name = new TextBox();
		save = new Button("Opslaan");
		saveAsNew = new Button("Opslaan als nieuwe categorie");
		delete = new Button("Verwijder");
		
		save.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				if (editingId == -1) {
					errorMsg.setText("U heeft geen categorie geselecteerd.");
				} else {
					Category cat = new Category(editingId,name.getText());
					myService.updateCategory(cat,updateCallback);
					myService.getCategories(getCallback);
				}
			}
		});
		saveAsNew.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				Category cat = new Category(editingId,name.getText());
				myService.insertCategory(cat,updateCallback);
				myService.getCategories(getCallback);
			}
		});
		delete.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) {
				myService.deleteCategory(editingId,updateCallback);
				myService.getCategories(getCallback);
			}
		});
		
		layoutTable.setWidget(0,0,errorMsg);
		layoutTable.setWidget(1,0,name);
		layoutTable.setWidget(1,1,save);
		layoutTable.setWidget(1,2,saveAsNew);
		layoutTable.setWidget(1,3,delete);
		
		TextColumn<Category> idColumn = new TextColumn<Category>() {
			@Override
			public String getValue(Category cat) {
				return ""+cat.getId();
			}
		};
		TextColumn<Category> titleColumn = new TextColumn<Category>() {
			@Override
			public String getValue(Category cat) {
				return cat.getTitle();
			}
		};
		table.setStyleName("tableCategories");
		table.addColumn(idColumn);
		table.addColumn(titleColumn);
		
		final SingleSelectionModel<Category> selectionModel = new SingleSelectionModel<Category>();
		table.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			public void onSelectionChange(SelectionChangeEvent event) {
				Category selected = selectionModel.getSelectedObject();
				editingId = selected.getId();
				name.setText(selected.getTitle());
			}
		});
		try {
			myService.getCategories(getCallback);
		} catch(Exception e) {
			errorMsg.setText("Er ging iets mis met het ophalen van de categorien.");
			e.printStackTrace();
		}
		layoutTable.setWidget(2,0,table);
		
		//fill p
		p.add(layoutTable);
		return p;
	}
}